<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>recordfunction.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="local_closure_goog_testing_recordfunction.js.html">recordfunction.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2010 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Helper class for recording the calls of a function.
<a name="line17"></a> *
<a name="line18"></a> * Example:
<a name="line19"></a> * &lt;pre&gt;
<a name="line20"></a> * var stubs = new goog.testing.PropertyReplacer();
<a name="line21"></a> *
<a name="line22"></a> * function tearDown() {
<a name="line23"></a> *   stubs.reset();
<a name="line24"></a> * }
<a name="line25"></a> *
<a name="line26"></a> * function testShuffle() {
<a name="line27"></a> *   stubs.set(Math, &#39;random&#39;, goog.testing.recordFunction(Math.random));
<a name="line28"></a> *   var arr = shuffle([1, 2, 3, 4, 5]);
<a name="line29"></a> *   assertSameElements([1, 2, 3, 4, 5], arr);
<a name="line30"></a> *   assertEquals(4, Math.random.getCallCount());
<a name="line31"></a> * }
<a name="line32"></a> *
<a name="line33"></a> * function testOpenDialog() {
<a name="line34"></a> *   stubs.set(goog.ui, &#39;Dialog&#39;,
<a name="line35"></a> *       goog.testing.recordConstructor(goog.ui.Dialog));
<a name="line36"></a> *   openConfirmDialog();
<a name="line37"></a> *   var lastDialogInstance = goog.ui.Dialog.getLastCall().getThis();
<a name="line38"></a> *   assertEquals(&#39;confirm&#39;, lastDialogInstance.getTitle());
<a name="line39"></a> * }
<a name="line40"></a> * &lt;/pre&gt;
<a name="line41"></a> *
<a name="line42"></a> */
<a name="line43"></a>
<a name="line44"></a>goog.provide(&#39;goog.testing.FunctionCall&#39;);
<a name="line45"></a>goog.provide(&#39;goog.testing.recordConstructor&#39;);
<a name="line46"></a>goog.provide(&#39;goog.testing.recordFunction&#39;);
<a name="line47"></a>
<a name="line48"></a>goog.require(&#39;goog.testing.asserts&#39;);
<a name="line49"></a>
<a name="line50"></a>
<a name="line51"></a>/**
<a name="line52"></a> * Wraps the function into another one which calls the inner function and
<a name="line53"></a> * records its calls. The recorded function will have 3 static methods:
<a name="line54"></a> * {@code getCallCount}, {@code getCalls} and {@code getLastCall} but won&#39;t
<a name="line55"></a> * inherit the original function&#39;s prototype and static fields.
<a name="line56"></a> *
<a name="line57"></a> * @param {!Function=} opt_f The function to wrap and record. Defaults to
<a name="line58"></a> *     {@link goog.nullFunction}.
<a name="line59"></a> * @return {!Function} The wrapped function.
<a name="line60"></a> */
<a name="line61"></a>goog.testing.recordFunction = function(opt_f) {
<a name="line62"></a>  var f = opt_f || goog.nullFunction;
<a name="line63"></a>  var calls = [];
<a name="line64"></a>
<a name="line65"></a>  function recordedFunction() {
<a name="line66"></a>    try {
<a name="line67"></a>      var ret = f.apply(this, arguments);
<a name="line68"></a>      calls.push(new goog.testing.FunctionCall(f, this, arguments, ret, null));
<a name="line69"></a>      return ret;
<a name="line70"></a>    } catch (err) {
<a name="line71"></a>      calls.push(new goog.testing.FunctionCall(f, this, arguments, undefined,
<a name="line72"></a>          err));
<a name="line73"></a>      throw err;
<a name="line74"></a>    }
<a name="line75"></a>  }
<a name="line76"></a>
<a name="line77"></a>  /**
<a name="line78"></a>   * @return {number} Total number of calls.
<a name="line79"></a>   */
<a name="line80"></a>  recordedFunction.getCallCount = function() {
<a name="line81"></a>    return calls.length;
<a name="line82"></a>  };
<a name="line83"></a>
<a name="line84"></a>  /**
<a name="line85"></a>   * Asserts that the function was called {@code expected} times.
<a name="line86"></a>   * @param {number} expected The expected number of calls.
<a name="line87"></a>   */
<a name="line88"></a>  recordedFunction.assertCallCount = function(expected) {
<a name="line89"></a>    var actual = calls.length;
<a name="line90"></a>    assertEquals(
<a name="line91"></a>        &#39;Expected &#39; + expected + &#39; call(s), but was &#39; + actual + &#39;.&#39;,
<a name="line92"></a>        expected, actual);
<a name="line93"></a>  };
<a name="line94"></a>
<a name="line95"></a>  /**
<a name="line96"></a>   * @return {!Array.&lt;!goog.testing.FunctionCall&gt;} All calls of the recorded
<a name="line97"></a>   *     function.
<a name="line98"></a>   */
<a name="line99"></a>  recordedFunction.getCalls = function() {
<a name="line100"></a>    return calls;
<a name="line101"></a>  };
<a name="line102"></a>
<a name="line103"></a>
<a name="line104"></a>  /**
<a name="line105"></a>   * @return {goog.testing.FunctionCall} Last call of the recorded function or
<a name="line106"></a>   *     null if it hasn&#39;t been called.
<a name="line107"></a>   */
<a name="line108"></a>  recordedFunction.getLastCall = function() {
<a name="line109"></a>    return calls[calls.length - 1] || null;
<a name="line110"></a>  };
<a name="line111"></a>
<a name="line112"></a>  /**
<a name="line113"></a>   * Returns and removes the last call of the recorded function.
<a name="line114"></a>   * @return {goog.testing.FunctionCall} Last call of the recorded function or
<a name="line115"></a>   *     null if it hasn&#39;t been called.
<a name="line116"></a>   */
<a name="line117"></a>  recordedFunction.popLastCall = function() {
<a name="line118"></a>    return calls.pop() || null;
<a name="line119"></a>  };
<a name="line120"></a>
<a name="line121"></a>  /**
<a name="line122"></a>   * Resets the recorded function and removes all calls.
<a name="line123"></a>   */
<a name="line124"></a>  recordedFunction.reset = function() {
<a name="line125"></a>    calls.length = 0;
<a name="line126"></a>  };
<a name="line127"></a>
<a name="line128"></a>  return recordedFunction;
<a name="line129"></a>};
<a name="line130"></a>
<a name="line131"></a>
<a name="line132"></a>/**
<a name="line133"></a> * Same as {@link goog.testing.recordFunction} but the recorded function will
<a name="line134"></a> * have the same prototype and static fields as the original one. It can be
<a name="line135"></a> * used with constructors.
<a name="line136"></a> *
<a name="line137"></a> * @param {!Function} ctor The function to wrap and record.
<a name="line138"></a> * @return {!Function} The wrapped function.
<a name="line139"></a> */
<a name="line140"></a>goog.testing.recordConstructor = function(ctor) {
<a name="line141"></a>  var recordedConstructor = goog.testing.recordFunction(ctor);
<a name="line142"></a>  recordedConstructor.prototype = ctor.prototype;
<a name="line143"></a>  goog.mixin(recordedConstructor, ctor);
<a name="line144"></a>  return recordedConstructor;
<a name="line145"></a>};
<a name="line146"></a>
<a name="line147"></a>
<a name="line148"></a>
<a name="line149"></a>/**
<a name="line150"></a> * Struct for a single function call.
<a name="line151"></a> * @param {!Function} func The called function.
<a name="line152"></a> * @param {!Object} thisContext {@code this} context of called function.
<a name="line153"></a> * @param {!Arguments} args Arguments of the called function.
<a name="line154"></a> * @param {*} ret Return value of the function or undefined in case of error.
<a name="line155"></a> * @param {*} error The error thrown by the function or null if none.
<a name="line156"></a> * @constructor
<a name="line157"></a> */
<a name="line158"></a>goog.testing.FunctionCall = function(func, thisContext, args, ret, error) {
<a name="line159"></a>  this.function_ = func;
<a name="line160"></a>  this.thisContext_ = thisContext;
<a name="line161"></a>  this.arguments_ = Array.prototype.slice.call(args);
<a name="line162"></a>  this.returnValue_ = ret;
<a name="line163"></a>  this.error_ = error;
<a name="line164"></a>};
<a name="line165"></a>
<a name="line166"></a>
<a name="line167"></a>/**
<a name="line168"></a> * @return {!Function} The called function.
<a name="line169"></a> */
<a name="line170"></a>goog.testing.FunctionCall.prototype.getFunction = function() {
<a name="line171"></a>  return this.function_;
<a name="line172"></a>};
<a name="line173"></a>
<a name="line174"></a>
<a name="line175"></a>/**
<a name="line176"></a> * @return {!Object} {@code this} context of called function. It is the same as
<a name="line177"></a> *     the created object if the function is a constructor.
<a name="line178"></a> */
<a name="line179"></a>goog.testing.FunctionCall.prototype.getThis = function() {
<a name="line180"></a>  return this.thisContext_;
<a name="line181"></a>};
<a name="line182"></a>
<a name="line183"></a>
<a name="line184"></a>/**
<a name="line185"></a> * @return {!Array} Arguments of the called function.
<a name="line186"></a> */
<a name="line187"></a>goog.testing.FunctionCall.prototype.getArguments = function() {
<a name="line188"></a>  return this.arguments_;
<a name="line189"></a>};
<a name="line190"></a>
<a name="line191"></a>
<a name="line192"></a>/**
<a name="line193"></a> * Returns the nth argument of the called function.
<a name="line194"></a> * @param {number} index 0-based index of the argument.
<a name="line195"></a> * @return {*} The argument value or undefined if there is no such argument.
<a name="line196"></a> */
<a name="line197"></a>goog.testing.FunctionCall.prototype.getArgument = function(index) {
<a name="line198"></a>  return this.arguments_[index];
<a name="line199"></a>};
<a name="line200"></a>
<a name="line201"></a>
<a name="line202"></a>/**
<a name="line203"></a> * @return {*} Return value of the function or undefined in case of error.
<a name="line204"></a> */
<a name="line205"></a>goog.testing.FunctionCall.prototype.getReturnValue = function() {
<a name="line206"></a>  return this.returnValue_;
<a name="line207"></a>};
<a name="line208"></a>
<a name="line209"></a>
<a name="line210"></a>/**
<a name="line211"></a> * @return {*} The error thrown by the function or null if none.
<a name="line212"></a> */
<a name="line213"></a>goog.testing.FunctionCall.prototype.getError = function() {
<a name="line214"></a>  return this.error_;
<a name="line215"></a>};
</pre>


</body>
</html>
